import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import player.PlayerFrame;;

public class PlayFrame extends JFrame implements ActionListener
{

	private JPanel contentPane;
	private JTextField txtPort;
	private JTextField textField_1;
	private JTextField txtNick;
	private JLabel lblNick;
	private String button;
	private JButton btnAction;
	private JComboBox comboBox;
	int port;
	PlayerFrame frame;
/**
	 * Create the frame.
	 */
	public PlayFrame(String option)
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 440, 302);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNrPortu = new JLabel("Nr Portu");
		lblNrPortu.setBounds(84, 45, 52, 14);
		contentPane.add(lblNrPortu);
		
		txtPort = new JTextField();
		txtPort.setText("4444");
		txtPort.setBounds(146, 42, 190, 20);
		contentPane.add(txtPort);
		txtPort.setColumns(4);
		
		if (option == "newPlay")
		{
			JLabel lblLiczbaGraczy = new JLabel("Liczba graczy");
			lblLiczbaGraczy.setBounds(58, 90, 78, 20);
			contentPane.add(lblLiczbaGraczy);
			
			comboBox = new JComboBox();
			comboBox.setModel(new DefaultComboBoxModel(new String[] {"2", "3", "4", "5", "6"}));
			comboBox.setBounds(146, 87, 42, 20);
			contentPane.add(comboBox);
			
			JLabel lblCoinsNumber = new JLabel("Ilosć żetonów");
			lblCoinsNumber.setBounds(58, 115, 78, 20);
			contentPane.add(lblCoinsNumber);
			
			textField_1 = new JTextField();
			textField_1.setBounds(146, 115, 190, 20);
			contentPane.add(textField_1);
			textField_1.setColumns(10);
			
			button = "Utwórz";
		}
		else
		{
			JLabel lblIP = new JLabel("IP");
			lblIP.setBounds(115, 90, 78, 20);
			contentPane.add(lblIP);
			
			textField_1 = new JTextField();
			textField_1.setBounds(140, 90, 190, 20);
			contentPane.add(textField_1);
			// ile cyfr ma IP?
			textField_1.setColumns(10); 
			
			button = "Dołącz";
		}
		lblNick = new JLabel("Nazwa Gracza");
		lblNick.setBounds(58, 150, 98, 20);
		contentPane.add(lblNick);
		
		txtNick = new JTextField();
		txtNick.setBounds(146, 150, 190, 20);
		contentPane.add(txtNick);
		txtNick.setColumns(10);
		
		btnAction = new JButton(button);
		btnAction.addActionListener(this);
		
		btnAction.setBounds(146, 193, 190, 23);
		contentPane.add(btnAction);
	}
	public void actionPerformed(ActionEvent event)
	{
		if (event.getActionCommand()=="Utwórz" )
		{
			try
			{   
				port = Integer.parseInt(txtPort.getText());
				int players  = Integer.parseInt((String)comboBox.getSelectedItem());
				int coins = Integer.parseInt(textField_1.getText());
				Game game = new Game(players,coins,port,txtNick.getText(),this);
				port = Integer.parseInt(txtPort.getText());
				frame = new PlayerFrame(port,"localhost",txtNick.getText());
				frame.setVisible(true);
			} 
			catch (NumberFormatException e)
			{
				System.out.println("Nieprawidlowe dane. ");
			}
		}
		else
		{
			try
			{
				port = Integer.parseInt(txtPort.getText());
				frame = new PlayerFrame(port,textField_1.getText(),txtNick.getText());
				frame.setVisible(true);
			}
			catch( NumberFormatException e)
			{
				System.out.println("Nieprawidłowy port");
			}
			
		}
	}	
}
